package com.jinxin.platform.cdsmsmgr.exception;

import com.jinxin.platform.base.common.pojo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ValidationException;

/**
 * 统一异常处理
 */
@Slf4j
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice("com.jinxin.platform.cdsmsmgr")
public class SmsMgrException {

  /**
   * 系统异常
   * @param exception
   * @param request
   * @return
   */
  @ExceptionHandler(value = Exception.class)
  public JsonResult Exception(Exception exception, HttpServletRequest request) {
    log.error("[该模块]--> 请求:{}, 异常:{}", request.getRequestURL(), exception);
    return JsonResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "系统异常");
  }

  /**
   * 自定义业务异常
   * @param exception
   * @param request
   * @return
   */
  @ExceptionHandler(value = BusinessRuntimeException.class)
  public JsonResult BusinessRuntimeException(BusinessRuntimeException exception, HttpServletRequest request) {
    log.error("[该模块]--> 请求:{}, 业务调用异常:{}", request.getRequestURL(), exception.getMessage());
    return JsonResult.error(exception.getCode(), exception.getMessage());
  }

  /**
   * 数据检验异常
   * ValidationException
   * @param e
   * @return
   */
  @ExceptionHandler(value = ValidationException.class)
  public JsonResult handException(ValidationException e) {
    log.info("[该模块]--> 参数检验失败--> {}", e.getMessage());
    return JsonResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
  }

}
